package com.ahdy.service.impl;

import com.ahdy.entity.MedicalRecordAnalysis;
import com.ahdy.mapper.MedicalRecordAnalysisMapper;
import com.ahdy.service.MedicalRecordAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 病历分析记录服务实现类
 */
@Service
public class MedicalRecordAnalysisServiceImpl implements MedicalRecordAnalysisService {
    
    @Autowired
    private MedicalRecordAnalysisMapper medicalRecordAnalysisMapper;
    
    @Override
    @Transactional
    public Integer add(MedicalRecordAnalysis record) {
        if (record.getCreatedAt() == null) {
            record.setCreatedAt(new Date());
        }
        record.setUpdatedAt(new Date());
        medicalRecordAnalysisMapper.insert(record);
        return record.getId();
    }
    
    @Override
    public MedicalRecordAnalysis getById(Integer id) {
        return medicalRecordAnalysisMapper.selectById(id);
    }
    
    @Override
    public List<MedicalRecordAnalysis> getByElderlyId(Integer elderlyId) {
        return medicalRecordAnalysisMapper.selectByElderlyId(elderlyId);
    }
    
    @Override
    @Transactional
    public boolean update(MedicalRecordAnalysis record) {
        record.setUpdatedAt(new Date());
        return medicalRecordAnalysisMapper.update(record) > 0;
    }
    
    @Override
    @Transactional
    public boolean delete(Integer id) {
        return medicalRecordAnalysisMapper.deleteById(id) > 0;
    }
    
    @Override
    public Map<String, Object> page(Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        
        // 获取分页参数
        Integer pageNum = params.get("pageNum") != null ? 
                Integer.parseInt(params.get("pageNum").toString()) : 1;
        Integer pageSize = params.get("pageSize") != null ? 
                Integer.parseInt(params.get("pageSize").toString()) : 10;
        String status = (String) params.get("status");
        
        // 计算偏移量
        Integer offset = (pageNum - 1) * pageSize;
        
        // 查询数据
        List<MedicalRecordAnalysis> list = medicalRecordAnalysisMapper.selectByPage(
                status, offset, pageSize);
        int total = medicalRecordAnalysisMapper.countTotal(status);
        
        result.put("list", list);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        
        return result;
    }
    
    @Override
    @Transactional
    public MedicalRecordAnalysis analyzeAndSave(Integer elderlyId, String imagePath) {
        // 创建病历分析记录
        MedicalRecordAnalysis record = new MedicalRecordAnalysis();
        record.setElderlyId(elderlyId);
        record.setImagePath(imagePath);
        record.setUploadDate(new Date());
        record.setStatus("待分析");
        
        // 保存记录
        this.add(record);
        
        try {
            // TODO: 调用AI分析服务进行分析
            // 模拟分析过程
            Thread.sleep(2000);
            
            // 更新分析结果
            record.setAnalysisResult("病历分析结果示例");
            record.setRecommendation("建议进行全面体检，并注意饮食和休息");
            record.setRelatedDiseases("高血压,糖尿病");
            record.setStatus("已完成");
            record.setAnalysisTime(new Date());
            
            this.update(record);
        } catch (Exception e) {
            // 分析失败
            record.setStatus("分析失败");
            this.update(record);
            throw new RuntimeException("病历分析失败", e);
        }
        
        return record;
    }
} 